package com.example.dllo.moondog.tools;

import android.graphics.Bitmap;
import android.util.LruCache;

import com.android.volley.toolbox.ImageLoader;

/**
 * Created by dllo on 16/1/18.
 */
public class MemoryCache implements ImageLoader.ImageCache {

    private LruCache<String,Bitmap> cache;

    public MemoryCache() {
        initMemoryCache();
    }

    public void initMemoryCache() {
        Long maxMemory = (Long) (Runtime.getRuntime().maxMemory());
        int cacheSize = (int) (maxMemory / 5);
        cache = new LruCache<String,Bitmap>(cacheSize){
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
            }
        };

    }
    @Override
    public Bitmap getBitmap(String url) {
        return cache.get(url);
    }

    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        cache.put(url,bitmap);
    }
}
